package com.smartcommunity.controller.tag;

import com.smartcommunity.bean.model.Pager;
import com.smartcommunity.bean.model.Result;
import com.smartcommunity.controller.BaseController;
import com.smartcommunity.entity.tag.Tag;
import com.smartcommunity.entity.tag.TagTarget;
import com.smartcommunity.entity.tag.dto.AddTagDto;
import com.smartcommunity.entity.tag.dto.QueryTagDto;
import com.smartcommunity.entity.tag.dto.UpdateTagDto;
import com.smartcommunity.log.interceptor.Log;
import com.smartcommunity.service.tag.TagService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: BMSZHD2
 * Date: 2019-06-21
 * Time: 16:13
 */
@Api(value = "标签", tags = {"标签管理"})
@RestController
@RequestMapping(value = "/tag")
@Slf4j
public class TagController extends BaseController {
    @Autowired
    TagService tagService;

    /**
     * 获取标签组列表
     * @return
     */
    @ApiOperation(httpMethod="GET",value = "获取标签群组",notes = "获取标签群组")
    @GetMapping(value = "/getTagTargetList")
    public Map<Integer,TagTarget> getTagTargetList(){
        return tagService.getTagTargetList();
    }

    /**
     * 获取标签列表
     * @param bean
     * @return
     */
    @ApiOperation(httpMethod="POST",value = "获取标签列表",notes = "获取标签列表")
    @PostMapping(value = "/getTagList")
    public Pager<Tag> getTagList(@Valid @RequestBody @ApiParam(name = "bean", value = "组对象") QueryTagDto bean){
        Map<String,Object> queryMap = new HashMap<String,Object>();
        queryMap.put("groupId",bean.getGroupId());
        queryMap.put("targetType",bean.getTargetType());
        queryMap.put("labelName",bean.getLabelName());
        return tagService.getTagList(queryMap,getPageNumber(bean.getPageNumber()),getPageSize(bean.getPageSize()));
    }

    /**
     * 增加非公开标签
     *
     * @param bean   角标签对象
     * @return
     */
    @Log(info = "新增标签信息",module = "标签管理",function = "新增")
    @ApiOperation("增加标签")
    @PostMapping(value = "/addTag")
    public Result addTag(@Valid @RequestBody @ApiParam(name = "bean", value = "组对象") AddTagDto bean) {
        return tagService.addTag(bean);

    }

    @Log(info = "修改标签信息",module = "标签管理",function = "修改")
    @ApiOperation("修改标签")
    @PostMapping(value = "/updateTag")
    public Result updateTag(@Valid @RequestBody @ApiParam(name = "bean", value = "组对象") UpdateTagDto bean) {
        return tagService.updateTag(bean);
    }


    /**
     * 删除标签
     * @param tagIds
     * @return
     */
    @Log(info = "删除标签信息",module = "标签管理",function = "删除")
    @ApiOperation("删除标签")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "tagIds", value = "标签id，多个以逗号分隔", required = true)
    })
    @PostMapping(value = "/deleteTag")
    public Result deleteTag(@RequestParam(name = "tagIds") String tagIds){
        return  tagService.deleteTag(tagIds);
    }


    @GetMapping(value = "/getObjectTag")
    public List<Tag> getObjectTag(@RequestParam(name = "objectId") String objectId,
                                  @RequestParam(required=false,name = "groupId") String groupId) {
        Map<String,Object> queryParam = new HashMap<String,Object>();
        queryParam.put("objectId",objectId);
        queryParam.put("groupId",groupId);
        return tagService.getObjectTag(queryParam);
    }

    @PostMapping(value = "/addObjectTag")
    public Result addObjectTag(@RequestParam(name = "objectId") String objectId,
                               @RequestParam(required = false,name = "villageCode") String villageCode,
                               @RequestParam(name = "tagList") List<Integer> tagList){
        Result res = new Result();
        try{
            tagService.addObjectTag(objectId,villageCode,tagList);
            res.setResultCode(200);
            res.setResultMessage("添加标签成功");
        }catch(Exception e){
            res.setResultCode(500);
            res.setResultMessage(e.getMessage());
        }
        return res;

    }
}
